'use strict';

const Controller = require('egg').Controller;

class DirinsmsgController extends Controller {
    // 创建控制器——POST——/name
    async create() {
        const { ctx } = this;
        const { ins_id, content, dir_id, topic_id } = ctx.request.body;
        const msg = {
            ins_id,
            content,
            dir_id,
            topic_id
        }
        const result = await ctx.service.dirinsmsg.add(msg);

        ctx.status = 201;
        ctx.body = result;
        return result;
    }
    // // 查询获取数据用控制器——GET——/name?keywords=?&&.....
    async index() {
        const { ctx } = this;
        const keywords = ctx.query.keywords ? ctx.query.keywords : '';
        const pageIndex = ctx.query.pageIndex ? +ctx.query.pageIndex : 1;
        const pageSize = ctx.query.pageSize ? +ctx.query.pageSize : 5;
        console.log('keywords', keywords);
        const result = await ctx.service.dirinsmsg.findList(keywords, pageIndex, pageSize);
        ctx.status = 200;
        ctx.body = result;
    }
    // // // 根据id获取信息的控制器方法——GET——/name/:id
    async show() {
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.dirinsmsg.findById(id);
        ctx.status = 200;
        ctx.body = result;
    }

    async findbytopicid() {
        const { ctx } = this;
        const topic_id = ctx.params.id;
        console.log('ctx.params.id',ctx.params.id);
        const result = await ctx.service.dirinsmsg.findByTopicId(topic_id);
        ctx.status = 200;
        ctx.body = result;
    }
}
module.exports = DirinsmsgController;
